﻿using cuiail.DBConnections;
using cuiail.DBModels;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Routing;
using System.Data;

namespace cuiail.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class CloudPlatformController : ControllerBase
    {
        private readonly IWorkingModeService _workingModeService;
        private readonly ICurrentWorkModeService _currentWorkModeService;
        private readonly IPrintContentService _printContentService;
        private readonly ILogger<LabController> _logger;

        public static CloudPlatformPosCommand cloudSystemCommand { get; set; } = new CloudPlatformPosCommand();

        public static CloudPlatPosMode cloudSystemModel { get; set; } = new CloudPlatPosMode();
        public static CloudPlatformPosWorkOrder cloudPlatformPosWorkOrder { get; set; } = new CloudPlatformPosWorkOrder();

        public static CloudPlatGetWorkOrderData cloudWorkOrder { get; set; } = new CloudPlatGetWorkOrderData();
        public static CloudPlatGetAssemblyIslandStatusData cloudAssemblyIslandStatus { get; set; } = new CloudPlatGetAssemblyIslandStatusData();
        public static CloudPlatGetTransportRobotStatusData cloudTransportRobotStatus { get; set; } = new CloudPlatGetTransportRobotStatusData();
        public static CloudPlatGetInspectionIslandStatusData cloudInspectionIslandStatus { get; set; } = new CloudPlatGetInspectionIslandStatusData();
        public static CloudPlatGetInspectionRobotStatusData cloudInspectionRobotStatus { get; set; } = new CloudPlatGetInspectionRobotStatusData();



        public CloudPlatformController(IWorkingModeService workingModeService,ICurrentWorkModeService currentWorkModeService, IPrintContentService printContentService, ILogger<LabController> logger)
        {
            _currentWorkModeService = currentWorkModeService;
            _workingModeService = workingModeService;
            _printContentService= printContentService;
            _logger = logger;
        }


        /// <summary>
        /// 获取整体工单数据
        /// </summary>
        [HttpGet]
        [Route("GetAllWorkOrderData")]
        public async Task<ActionResult<IEnumerable<CloudPlatGetWorkOrderData>>> GetAllWorkOrderDataAsync()
        {
            try
            {
                _工单管理类.UpdateWorkOrderProcessStatus(cloudWorkOrder);
                var result = cloudWorkOrder;
                return Ok(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "云平台get整体工单数据发生错误");
                return StatusCode(500, "云平台get整体工单数据发生错误，请稍后重试");
            }
        }


        /// <summary>
        /// 获取装配岛状态数据
        /// </summary>
        [HttpGet]
        [Route("GetAssemblyIslandStatusData")]
        public async Task<ActionResult<IEnumerable< CloudPlatGetAssemblyIslandStatusData>>> GetAssemblyIslandStatusDataAsync()
        {
            try
            {
                var result = cloudAssemblyIslandStatus;
                return Ok(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "云平台get装配岛状态数据发生错误");
                return StatusCode(500, "云平台get装配岛状态数据发生错误，请稍后重试");
            }
        }


        /// <summary>
        /// 获取搬运机器人状态数据
        /// </summary>
        [HttpGet]
        [Route("GetTransportRobotStatusData")]
        public async Task<ActionResult<IEnumerable<CloudPlatGetTransportRobotStatusData>>> GetTransportRobotStatusDataAsync()
        {
            try
            {
               var result = cloudTransportRobotStatus;
                return Ok(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "云平台get搬运机器人状态数据发生错误");
                return StatusCode(500, "云平台get搬运机器人状态数据时发生错误，请稍后重试");
            }
        }

        /// <summary>
        /// 获取质检岛状态数据
        /// </summary>
        [HttpGet]
        [Route("GetQualityIslandStatusData")]
        public async Task<ActionResult<IEnumerable<CloudPlatGetInspectionIslandStatusData>>> GetQualityIslandStatusDataAsync()
        {
            try
            {
                var result =cloudInspectionIslandStatus;
                return Ok(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "云平台get质检岛状态数据发生错误");
                return StatusCode(500, "云平台get质检岛状态数据时发生错误，请稍后重试");
            }
        }



        /// <summary>
        /// 获取巡检机器人状态数据
        /// </summary>
        [HttpGet]
        [Route("GetInspectionRobotStatusData")]
        public async Task<ActionResult<IEnumerable<CloudPlatGetInspectionRobotStatusData>>> GetInspectionRobotStatusDataAsync()
        {
            try
            {
                var result = cloudInspectionRobotStatus;
                return Ok(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "云平台get巡检机器人状态数据发生错误");
                return StatusCode(500, "云平台get巡检机器人状态数据时发生错误，请稍后重试");
            }
        }

        /// </summary>
        /// 获取当前工序流程
        /// 
        [HttpGet]
        [Route("GetCurrentModeprocesslist")]
        public async Task<ActionResult<IEnumerable<ModeProcessList>>> GetCurrentModeprocesslistAsync()
        {
            try
            {
                ModeProcessList modeProcessList = null;
                if (CloudPlatformController.cloudSystemModel.WorkingModeId==SystemMode.InteractiveMode)
                {
                    modeProcessList = ModeProcessList.InteractiveMode;
                }
                else if (CloudPlatformController.cloudSystemModel.WorkingModeId==SystemMode.LoopDemoMode)
                {
                    modeProcessList = ModeProcessList.LoopDemoMode;
                }
                else if (CloudPlatformController.cloudSystemModel.WorkingModeId==SystemMode.SingleDemoMode)
                {
                    modeProcessList = ModeProcessList.SingleDemoMode;
                }
                if(modeProcessList == null)
                {
                    return StatusCode(500, "当前系统模式无对应工序流程");
                }
                else
                {
                    return Ok(modeProcessList);
                }
                    
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "云平台get状态数据发生错误");
                return StatusCode(500, "云平台get状态数据时发生错误，请稍后重试");
            }
        }

        /// <summary>
        /// 写入工单数据
        /// </summary>
        /// <param name="currentWorkingMode">当前工作模式代码</param>
        /// <returns>写入成功或者失败</returns>
        [HttpPost]
        [Route("InsertWorkOrderData")]
        public async Task<ActionResult<CommandStatusFeedback>> InsertWorkOrderDataAsync(CloudPlatformPosWorkOrder temp)
        {
            try
            {
               
                if (string.IsNullOrWhiteSpace(cloudPlatformPosWorkOrder.WorkOrderNumber))
                {
                    cloudPlatformPosWorkOrder = temp;
                    cloudWorkOrder.InitFromPosdata(temp);
                }
                if (cloudWorkOrder.WorkOrderCompletionStatus == "已完成"||cloudWorkOrder.WorkOrderCompletionStatus=="未开始")
                {
                    cloudPlatformPosWorkOrder = temp;
                    cloudWorkOrder.InitFromPosdata(temp);
                    var result = new CommandStatusFeedback(true, "工单接收正常");
                    return Ok(result);
                }
                else
                {
                    var result = new CommandStatusFeedback(false, "还有工单在进行");
                    return Ok(result);
                }
                    
                  
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "写入工单数据时发生错误");
                return StatusCode(500, "写入工单数据时发生错误，请稍后重试");
            }
        }



        /// <summary>
        /// 写入系统模式数据
        /// </summary>
        /// <param name="currentWorkingMode">当前工作模式代码</param>
        /// <returns>写入成功或者失败</returns>
        [HttpPost]
        [Route("InsertSystemModel")]
        public async Task<ActionResult<bool>> InsertSystemModelAsync(CloudPlatPosMode temp)
        {
            try
            {
                cloudSystemModel = temp;
                var result = true;
                return Ok(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "写入系统模式数据时发生错误");
                return StatusCode(500, "写入系统模式数据时发生错误，请稍后重试");
            }
        }



        /// <summary>
        /// 写入系统命令
        /// </summary>
        /// <param name="currentWorkingMode">当前工作模式代码</param>
        /// <returns>写入成功或者失败</returns>
        [HttpPost]
        [Route("InsertSystemCommand")]
        public async Task<ActionResult<bool>> InsertSystemCommandAsync(CloudPlatformPosCommand temp)
        {
            try
            {
                cloudSystemCommand = temp;
                var result = true;
                return Ok(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "写入系统命令时发生错误");
                return StatusCode(500, "写入系统命令时发生错误，请稍后重试");
            }
        }
        //写入工单命令
        /// <summary>
        /// 写入工单命令
        /// </summary>
        /// <param name="currentWorkingMode">当前工作模式代码</param>
        /// <returns>写入成功或者失败</returns>
        [HttpPost]
        [Route("WriteWorkOrderCommand")]
        public async Task<ActionResult<CommandStatusFeedback>> WriteWorkOrderCommandAsync(WorkOrderCommand command)
        {
            try
            {
                switch(command)
                    {
                    case WorkOrderCommand.Start:
                        if (CloudPlatformController.cloudWorkOrder.WorkOrderCompletionStatus!="未开始" &&CloudPlatformController.cloudWorkOrder.WorkOrderCompletionStatus!="已完成")
                        {

                            return Ok(new CommandStatusFeedback(false, "已有工单进行，或请下发"));
                        }
                        _工单管理类.WorkOrderStart();
                        return Ok(new CommandStatusFeedback(true, ""));
                        break;
                  
                    case WorkOrderCommand.StopAndEnd:
                        _工单管理类.WorkOrderStopAndEnd();
                        return Ok(new CommandStatusFeedback(true, ""));
                        break;
                    case WorkOrderCommand.ResumeFromStart:
                        return Ok(new CommandStatusFeedback(false,"该功能未实现"));
                        break;
                    case WorkOrderCommand.Reset:
                        return Ok(new CommandStatusFeedback(false, "该功能未实现"));
                        break;
                }
                return Ok(new CommandStatusFeedback(true, ""));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "写入系统命令时发生错误");
                return StatusCode(500, "写入系统命令时发生错误，请稍后重试");
            }
        }







    }
}
